<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>12. 配置以及生成表单 | 被删的前端游乐场</title>
    <meta name="description" content="Just playing around">
    
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.a683ed26.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.e5cf4092.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.c853767c.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.eed49fdd.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/52.8478d163.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.a683ed26.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-0cd75e06><span class="stand" data-v-0cd75e06></span> <div class="cat" data-v-0cd75e06><div class="body" data-v-0cd75e06></div> <div class="head" data-v-0cd75e06><div class="ear" data-v-0cd75e06></div> <div class="ear" data-v-0cd75e06></div></div> <div class="face" data-v-0cd75e06><div class="nose" data-v-0cd75e06></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div></div> <div class="tail-container" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>玩转 Angular</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/angular-free/0-about-angular-from-1-to-4.html" class="sidebar-link">谈谈 AngularJS 到 Angular</a></li><li><a href="/front-end-playground/front-end-others/angular-free/0-di-in-angular.html" class="sidebar-link">谈谈 Angular 的依赖注入</a></li><li><a href="/front-end-playground/front-end-others/angular-free/1-init-angular-app-with-webpack.html" class="sidebar-link">1. 用 Webpack 启动 Angular 应用</a></li><li><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html" class="sidebar-link">2. 改善应用配置</a></li><li><a href="/front-end-playground/front-end-others/angular-free/3-init-routes-and-assets.html" class="sidebar-link">3. 启用路由和添加静态资源</a></li><li><a href="/front-end-playground/front-end-others/angular-free/4-create-sidebar.html" class="sidebar-link">4. 制作左侧自动定位菜单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/5-custom-input-component.html" class="sidebar-link">5. 自定义 input 表单控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/6-two-form-building.html" class="sidebar-link">6. 模型驱动和模板驱动的表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/7-dynamic-form.html" class="sidebar-link">7. 创建动态表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/8-radio-and-checkbox.html" class="sidebar-link">8. 表单的 radio 和 checkbox</a></li><li><a href="/front-end-playground/front-end-others/angular-free/9-upload-iamge.html" class="sidebar-link">9. 图片上传控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html" class="sidebar-link">10. 向表单添加条件控制</a></li><li><a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html" class="sidebar-link">11. 使用动态表单制作选项配置对话框</a></li><li><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html" class="active sidebar-link">12. 配置以及生成表单</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html#自定义表单作为表单控件" class="sidebar-link">自定义表单作为表单控件</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html#配置表单" class="sidebar-link">配置表单</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html#配置生成表单" class="sidebar-link">配置生成表单</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html#结束语" class="sidebar-link">结束语</a></li></ul></li><li><a href="/front-end-playground/front-end-others/angular-free/13-dynamic-list.html" class="sidebar-link">13. 动态列表配置</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>BOX2D.js 文档与教程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>该系列用于探索一些 Angular 中灵活或者新的用法。本文讲述我们使用表单生成的功能，来进行表单的自定义配置和生成。</p> <h2 id="自定义表单作为表单控件"><a href="#自定义表单作为表单控件" aria-hidden="true" class="header-anchor">#</a> 自定义表单作为表单控件</h2> <h3 id="option-dialog"><a href="#option-dialog" aria-hidden="true" class="header-anchor">#</a> option-dialog</h3> <p>上一节<a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html">《11. 使用动态表单制作选项配置对话框》</a>我们讲了使用动态表单制作选项配置对话框，这里面我们需要将这样的组件重新作为表单的一部分，在我们的自定义表单中使用。
我们需要调整来使得以下方式可用：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>option-dialog</span>
  <span class="token attr-name">[type]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>optionsType(control.type)<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">[formControlName]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.key<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>option-dialog</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><p>这时候我们又需要前面的自定义 input 神奇了：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span>customInputAccessor<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'../../class/custom-input.class'</span><span class="token punctuation">;</span>
@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    providers<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token function">customInputAccessor</span><span class="token punctuation">(</span>OptionDialogComponent<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">OptionDialogComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> type<span class="token punctuation">:</span> string <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
    optionsForm<span class="token punctuation">:</span> IOption<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    optionControl<span class="token punctuation">:</span> ICustomControl<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    isShown<span class="token punctuation">:</span> boolean <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> model<span class="token punctuation">:</span> IOptions<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 控件的值</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onChange</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token parameter">_<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onTouched</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
    <span class="token comment">// ...其他省略</span>
    <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">isValid</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">setOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">addControl</span><span class="token punctuation">(</span>

    <span class="token function">saveOptions</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 将valid过滤掉</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>model <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>optionsForm<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">op<span class="token punctuation">:</span> IOption</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> <span class="token punctuation">{</span>id<span class="token punctuation">,</span> text<span class="token punctuation">}</span> <span class="token operator">=</span> op<span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token punctuation">{</span>id<span class="token punctuation">,</span> text<span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>isShown <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onChange</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 触发值更新</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 其他照旧</span>
    <span class="token function">onBlur</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onTouched</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">writeValue</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> IOptions<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> value<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>model <span class="token operator">=</span> value<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">registerOnChange</span><span class="token punctuation">(</span><span class="token function-variable function">fn</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token parameter">_<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>onChange <span class="token operator">=</span> fn<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">registerOnTouched</span><span class="token punctuation">(</span><span class="token function-variable function">fn</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>onTouched <span class="token operator">=</span> fn<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="hidden-when-dialog"><a href="#hidden-when-dialog" aria-hidden="true" class="header-anchor">#</a> hidden-when-dialog</h3> <p>我们的配置选项中，有一个是<code>hiddenWhen</code>参数，该参数也是通过配置多项条件的方式，这里我们也添加一个<code>hidden-when-dialog</code>来配置。
由于篇幅关系，这里只看看配置文件：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">const</span> validationsFormControl<span class="token punctuation">:</span> ICustomControl<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;key必填&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;validate&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;validate&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;validate必选&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;&gt;&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;&gt;&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;&gt;=&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;&gt;=&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;&lt;&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;&lt;&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;&lt;=&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;&lt;=&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;==&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;==&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;===&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;===&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;!==&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;!==&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;indexOf&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;indexOf&quot;</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;param&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;param&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;param必填&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p>至于组件的 html 和 js 大家可以翻阅项目代码（在文章最后有链接）。</p> <h2 id="配置表单"><a href="#配置表单" aria-hidden="true" class="header-anchor">#</a> 配置表单</h2> <h3 id="表单的选项和结构"><a href="#表单的选项和结构" aria-hidden="true" class="header-anchor">#</a> 表单的选项和结构</h3> <p>目前为止，我们的表单控件格式大致是下面这些：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">ICustomControl</span> <span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  label<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  key<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  validations<span class="token operator">?</span><span class="token punctuation">:</span> IValidations<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  options<span class="token operator">?</span><span class="token punctuation">:</span> IOptions<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  limit<span class="token operator">?</span><span class="token punctuation">:</span> ILimit<span class="token punctuation">;</span>
  hiddenWhen<span class="token operator">?</span><span class="token punctuation">:</span> IHiddenCondition<span class="token punctuation">;</span>
  description<span class="token operator">?</span><span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  setOptions<span class="token operator">?</span><span class="token punctuation">:</span> boolean<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>我们需要添加一些选项或者是输入，来让使用的人生成以上的格式，然后生成想要的表单。
这里面需要详细配置的有：</p> <ul><li><code>validations</code> <ul><li>校验方式，如<code>required</code>、<code>maxLength</code>等</li> <li>这里可以使用<code>checkbox-with-input</code>的方式来配置</li></ul></li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IValidations</span> <span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  param<span class="token operator">?</span><span class="token punctuation">:</span> any<span class="token punctuation">;</span>
  message<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li><code>options</code> <ul><li>选项配置，当选择的类型<code>type</code>需要配置选项时出现</li> <li><code>type == 'select' || type == 'radio' || type == 'checkbox'</code>等等</li> <li>前面的<code>option-dialog</code>已封装该功能，并提供自定义表单使用，但目前只支持<code>id</code>和<code>text</code>的配置</li></ul></li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IOptions</span> <span class="token punctuation">{</span>
  id<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  text<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  withInput<span class="token operator">?</span><span class="token punctuation">:</span> boolean<span class="token punctuation">;</span>
  type<span class="token operator">?</span><span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;number&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li><code>limit</code> <ul><li>图片校验，只当选择图片上传时使用</li> <li>使用<code>checkbox-with-input</code>的方式来配置</li></ul></li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">ILimit</span> <span class="token punctuation">{</span>
  width<span class="token operator">?</span><span class="token punctuation">:</span> number<span class="token punctuation">;</span>
  height<span class="token operator">?</span><span class="token punctuation">:</span> number<span class="token punctuation">;</span>
  size<span class="token operator">?</span><span class="token punctuation">:</span> number<span class="token punctuation">;</span>
  type<span class="token operator">?</span><span class="token punctuation">:</span> string<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li><code>hiddenWhen</code> <ul><li>条件符合时隐藏，可配置多个条件，并设置多个条件的关系（<code>||</code>或<code>&amp;&amp;</code>）</li> <li><code>hidden-when-dialog</code></li></ul></li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IHiddenCondition</span> <span class="token punctuation">{</span>
  condition<span class="token punctuation">:</span> <span class="token string">&quot;||&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;&amp;&amp;&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
  validations<span class="token operator">?</span><span class="token punctuation">:</span> IHiddenValidate<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IHiddenValidate</span> <span class="token punctuation">{</span>
  key<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  validate<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  param<span class="token punctuation">:</span> string <span class="token operator">|</span> number<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="配置表单的配置"><a href="#配置表单的配置" aria-hidden="true" class="header-anchor">#</a> 配置表单的配置</h3> <p>接下来我们来配置生成这样一个用来生成配置的表单吧：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">const</span> normalFormControl<span class="token punctuation">:</span> ICustomControl<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;类型&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;类型必选&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;input-text&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;input-数字&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;radio组&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;checkbox&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;checkbox组&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;date&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;日期&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;date-time&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;日期+时间(分钟)&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;date-hour&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;日期+时间(小时)&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;upload-image&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;图片上传&quot;</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;字段key&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;字段key必填&quot;</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;maxLength&quot;</span><span class="token punctuation">,</span>
        param<span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;字段key最多15个字符&quot;</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;minLength&quot;</span><span class="token punctuation">,</span>
        param<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;字段key最少3个字符&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;标签说明&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;label&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;标签说明必填&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;checkbox-with-input&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;校验&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;validations&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;required&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;必填/必选&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;邮件格式&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;maxLength&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;最大字符数&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;minLength&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;最小字符数&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;checkbox-with-input&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;图片上传限制&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;limit&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;width&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;宽(px)&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;height&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;高(px)&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;size&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;大小&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;number&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;类型&quot;</span><span class="token punctuation">,</span> withInput<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    description<span class="token punctuation">:</span> <span class="token string">&quot;类型限制可填 jpg | png | gif&quot;</span><span class="token punctuation">,</span>
    hiddenWhen<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token comment">// 当选择上传图片组件才显示</span>
      condition<span class="token punctuation">:</span> <span class="token string">&quot;||&quot;</span><span class="token punctuation">,</span>
      validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;upload-image&quot;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;选项配置&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;options&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    hiddenWhen<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token comment">// 选择带选项组件显示</span>
      condition<span class="token punctuation">:</span> <span class="token string">&quot;&amp;&amp;&quot;</span><span class="token punctuation">,</span>
      validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;checkbox&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    setOptions<span class="token punctuation">:</span> <span class="token boolean">true</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;描述说明&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;description&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 带选项的可配置提供自定义选项</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;是否自定义选项&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;setOptions&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;否&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;withOption&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;是&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    hiddenWhen<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      condition<span class="token punctuation">:</span> <span class="token string">&quot;&amp;&amp;&quot;</span><span class="token punctuation">,</span>
      validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;select&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;checkbox&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;type&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 该字段用来判断是否启用下面配置，不作为最终生成的配置选项</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;是否启用条件隐藏&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;withValidation&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;否&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;是&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;hidden-when-dialog&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;隐藏条件配置&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;hiddenWhen&quot;</span><span class="token punctuation">,</span>
    hiddenWhen<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      condition<span class="token punctuation">:</span> <span class="token string">&quot;&amp;&amp;&quot;</span><span class="token punctuation">,</span>
      validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;withValidation&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;!=&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;1&quot;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p>大家会发现，上面的配置并不完全可以得到我们最后想要的配置，如<code>limit</code>和<code>options</code>的格式，故我们最后还需要输出时调整：</p> <div class="language-js extra-class"><pre class="language-js"><code>    <span class="token function">formJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> jsonResult <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>customForms<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">form</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token comment">// 取出需要使用的字段</span>
            <span class="token keyword">const</span> <span class="token punctuation">{</span>type<span class="token punctuation">,</span> label<span class="token punctuation">,</span> key<span class="token punctuation">,</span> limit<span class="token punctuation">,</span> validations<span class="token punctuation">,</span> options<span class="token punctuation">,</span> description<span class="token punctuation">,</span> setOptions<span class="token punctuation">,</span> withValidation<span class="token punctuation">,</span> hiddenWhen<span class="token punctuation">}</span> <span class="token operator">=</span> form<span class="token punctuation">;</span>
            <span class="token keyword">const</span> validationArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">const</span> limitObj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>validations<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>validations<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">valid</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                    <span class="token keyword">const</span> value <span class="token operator">=</span> validations<span class="token punctuation">[</span>valid<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
                    <span class="token keyword">let</span> message <span class="token operator">=</span> valid<span class="token punctuation">;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        message <span class="token operator">+=</span> <span class="token template-string"><span class="token string">`: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>value<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">`</span></span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>validations<span class="token punctuation">[</span>valid<span class="token punctuation">]</span><span class="token punctuation">.</span>checked<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        validationArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
                            type<span class="token punctuation">:</span> valid<span class="token punctuation">,</span>
                            param<span class="token punctuation">:</span> value <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">?</span> <span class="token keyword">undefined</span> <span class="token punctuation">:</span> <span class="token function">Number</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">,</span>
                            message
                        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>limit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>limit<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">con</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>limit<span class="token punctuation">[</span>con<span class="token punctuation">]</span><span class="token punctuation">.</span>checked<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        limitObj<span class="token punctuation">[</span>con<span class="token punctuation">]</span> <span class="token operator">=</span> limit<span class="token punctuation">[</span>con<span class="token punctuation">]</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>withValidation <span class="token operator">!=</span> <span class="token string">'1'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                hiddenWhen <span class="token operator">=</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            jsonResult<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
                limit<span class="token punctuation">:</span> limitObj<span class="token punctuation">,</span>
                validations<span class="token punctuation">:</span> validationArr<span class="token punctuation">,</span>
                type<span class="token punctuation">,</span> label<span class="token punctuation">,</span> key<span class="token punctuation">,</span> options<span class="token punctuation">,</span> description<span class="token punctuation">,</span> setOptions<span class="token punctuation">,</span> hiddenWhen
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>json <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span>jsonResult<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre></div><p>而我们的 html 模版长这样：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">=&quot;</span><span class="token attr-value"><span class="token property">margin-top</span><span class="token punctuation">:</span> 30px<span class="token punctuation">;</span></span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>自定义表单配置<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>btn btn-info<span class="token punctuation">&quot;</span></span> <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>addControl()<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>添加表单选项<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">*ngFor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>let form of customForms; let i = index;<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>表单控件{{i + 1}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dynamic-form</span> <span class="token attr-name">[config]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>customControl<span class="token punctuation">&quot;</span></span> <span class="token attr-name">[model]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>form<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dynamic-form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>btn btn-success<span class="token punctuation">&quot;</span></span> <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>formJson()<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>生成JSON<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>json-to-html</span> <span class="token attr-name">[hidden]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>!json<span class="token punctuation">&quot;</span></span> <span class="token attr-name">[(ngModel)]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>json<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>json-to-html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h2 id="配置生成表单"><a href="#配置生成表单" aria-hidden="true" class="header-anchor">#</a> 配置生成表单</h2> <h3 id="生成-json-展示"><a href="#生成-json-展示" aria-hidden="true" class="header-anchor">#</a> 生成 json 展示</h3> <p>上面大家也看到了<code>json-to-html</code>，其实主要拿以前实现的其中一种方法来生成带样式的 json，具体实现大家可以参考<a href="https://godbasin.github.io/2016/11/13/json-to-html-1-use-string-regular/" target="_blank" rel="noopener noreferrer">《将 json 输出为 html(一)：字符串正则匹配》<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>和<a href="https://godbasin.github.io/2016/11/13/json-to-html-2-use-object/" target="_blank" rel="noopener noreferrer">《将 json 输出为 html(二)：js 数据类型判断实现》<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p> <p>这里我们将它稍微封装一下：</p> <div class="language-js extra-class"><pre class="language-js"><code>@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    selector<span class="token punctuation">:</span> <span class="token string">'json-to-html'</span><span class="token punctuation">,</span>
    template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`
        &lt;div&gt;&lt;/div&gt;`</span></span><span class="token punctuation">,</span>
    providers<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token function">customInputAccessor</span><span class="token punctuation">(</span>JsonToHtmlComponent<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">JsonToHtmlComponent</span> <span class="token keyword">implements</span> <span class="token class-name">AfterViewInit</span> <span class="token punctuation">{</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> options<span class="token punctuation">:</span> object <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> isEdit<span class="token punctuation">:</span> boolean <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    @<span class="token function">Output</span><span class="token punctuation">(</span><span class="token punctuation">)</span> change <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EventEmitter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> editor<span class="token punctuation">:</span> any<span class="token punctuation">;</span>
    <span class="token keyword">private</span> el<span class="token punctuation">;</span>

    <span class="token keyword">private</span> model<span class="token punctuation">:</span> any <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 控件的值</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onChange</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token parameter">_<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onTouched</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

    <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token parameter">el<span class="token punctuation">:</span> ElementRef</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>el <span class="token operator">=</span> el<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">ngAfterViewInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">setValue</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>el<span class="token punctuation">.</span>nativeElement<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token function">JsonToHtml</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">writeValue</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> string<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>model <span class="token operator">=</span> value<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 其他照旧</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="读取配置"><a href="#读取配置" aria-hidden="true" class="header-anchor">#</a> 读取配置</h3> <p>我们将上面生成的 json 复制一下，粘贴到一个地方，然后就可以生成想要的表单了。
这里配置读取很简单：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">=&quot;</span><span class="token attr-value"><span class="token property">margin-top</span><span class="token punctuation">:</span> 30px<span class="token punctuation">;</span></span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>自定义表单生成<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>输入json<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>textarea</span> <span class="token attr-name">[(ngModel)]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>json<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>textarea</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>btn btn-success<span class="token punctuation">&quot;</span></span> <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>buildForm()<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>生成表单<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dynamic-form</span> <span class="token attr-name">*ngIf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>isShown<span class="token punctuation">&quot;</span></span> <span class="token attr-name">[config]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>customControl<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dynamic-form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><p>使用<code>*ngIf</code>主要是方便控制<code>[config]</code>的输入更改的时候重新生成，因为自定义表单通常不会动态改变，故我们不将该功能拓展进去。
生成配置也很简单：</p> <div class="language-js extra-class"><pre class="language-js"><code>    customControl<span class="token punctuation">:</span> ICustomControl<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    json<span class="token punctuation">:</span> any<span class="token punctuation">;</span>
    isShown<span class="token punctuation">:</span> boolean <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

    <span class="token function">buildForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>isShown <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token keyword">const</span> config <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>json<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>customControl <span class="token operator">=</span> config<span class="token punctuation">.</span>jsonResult<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>isShown <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre></div><p>这样，我们就能实现表单的创建和生成啦。效果图如下：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674116%281%29.png" alt="image"></p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674140%281%29.png" alt="image"></p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674168%281%29.png" alt="image"></p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674345%281%29.png" alt="image"></p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674356%281%29.png" alt="image"></p> <h2 id="结束语"><a href="#结束语" aria-hidden="true" class="header-anchor">#</a> 结束语</h2> <p>项目中表单配置部分大致的实现也是差不多告一段落啦，后面可能更多的是补充和优化而已。<br>
而其实项目还可以多种拓展呢，像这样的添加配置像（validations 和 hiddenWhen）也可以通过抽象做成更加通用的功能。<br> <a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-free/12-config-and-create-form" target="_blank" rel="noopener noreferrer">此处查看项目代码<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a><br> <a href="http://angular2-free.godbasin.com/12-config-and-create-form/index.html#/home/page-setting" target="_blank" rel="noopener noreferrer">此处查看页面效果<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p>完整项目也可查看<a href="https://github.com/godbasin/angular-custom-app" target="_blank" rel="noopener noreferrer">angular-custom-app 项目<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/angular-free/12-config-and-create-form.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html" class="prev">
          11. 使用动态表单制作选项配置对话框
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/angular-free/13-dynamic-list.html">
          13. 动态列表配置
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-7232" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty1.svg);"></div></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.e5cf4092.js" defer></script><script src="/front-end-playground/assets/js/3.c853767c.js" defer></script><script src="/front-end-playground/assets/js/2.eed49fdd.js" defer></script><script src="/front-end-playground/assets/js/52.8478d163.js" defer></script>
  </body>
</html>
